package com.persondemo.videoappliction.ui.duanzi.presenter;

import android.text.TextUtils;

import com.persondemo.videoappliction.bean.DuanziBean;
import com.persondemo.videoappliction.bean.ZanCaiBean;
import com.persondemo.videoappliction.net.BaseApi;
import com.persondemo.videoappliction.net.BaseObserver;
import com.persondemo.videoappliction.net.RxSchedulers;
import com.persondemo.videoappliction.ui.base.BasePresenter;
import com.persondemo.videoappliction.ui.duanzi.contract.DuanziContract;

import javax.inject.Inject;

/**
 * Created by Administrator on 2018/4/19.
 */

public class DuanziPresenter extends BasePresenter<DuanziContract.View> implements DuanziContract.Presenter {
    BaseApi baseApi;

    @Inject
    public DuanziPresenter(BaseApi baseApi) {
        this.baseApi = baseApi;
    }

    @Override
    public void getdata(String type, String page,String id) {
        baseApi.getduanzi(type, page,id)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new BaseObserver<DuanziBean>() {
                    @Override
                    public void onSuccess(DuanziBean duanziBean) {
                        DuanziBean.DataBean dataBean = null;
                        if (duanziBean != null && duanziBean.getStatus() == 1) {
                            for (int i = 0; i < duanziBean.getData().size(); i++) {
                                dataBean = duanziBean.getData().get(i);
                                if (duanziBean.getData().get(i).getTypes() == 1) {
                                    dataBean.itemType = DuanziBean.DataBean.types_01;
                                } else if ((duanziBean.getData().get(i).getTypes() == 2) ){
                                    dataBean.itemType = DuanziBean.DataBean.types_02;
                                }else {
                                    dataBean.itemType = DuanziBean.DataBean.types_03;
                                }
                            }
                        }
                        if (Integer.parseInt(page) > 1) {
                            mView.loadmore(duanziBean);
                        } else {
                            mView.loaddata(duanziBean);
                        }

                    }

                    @Override
                    public void onFail(Throwable e) {
                        mView.showFaild();
                    }
                });
    }

    @Override
    public void zancaiping(String ids, String type, String device) {
        baseApi.getinfor(ids, type, device)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new BaseObserver<ZanCaiBean>() {
                    @Override
                    public void onSuccess(ZanCaiBean zanCaiBean) {
                        mView.getzcp(zanCaiBean);
                    }

                    @Override
                    public void onFail(Throwable e) {
                        mView.showFaild();
                    }
                });
    }
}
